<template>
  <div class="ai-chat custom-el-plus">
    <div class="chat-list-wrapper">
      <div
        class="btn-group"
        v-if="newChatFlag || chatWithRoleFlag || historyChatFlag || allPromptFlag">
        <el-button @click="back2Home">提示词首页</el-button>
        <el-button @click="createNewChat">新建对话</el-button>
      </div>
      <div class="list-title">对话记录</div>
      <div class="chat-list">
        <ChatListItem
          v-for="(item, index) in dialogHistoryList"
          :key="index"
          :prompInfo="item"
          :chatNum="item.records"
          :isActive="item.conversationId === someHistoryInfo?.conversationId"
          @refreshChatList="getDialogHistoryList"
          @click="jumpToHistoryChat(item)"></ChatListItem>
      </div>
    </div>
    <div class="main-chat-wrapper">
      <ChatHome v-if="!newChatFlag && !allPromptFlag && !historyChatFlag && !chatWithRoleFlag"></ChatHome>
      <NewChat
        ref="newChatRef"
        :someHistoryInfo="someHistoryInfo"
        :chatRoleInfo="chatRoleInfo"
        :newChatFlag="newChatFlag"
        v-if="newChatFlag || chatWithRoleFlag || historyChatFlag"></NewChat>
      <PromptsList v-if="allPromptFlag"></PromptsList>
    </div>
  </div>
</template>

<script setup>
  import { getCurrentInstance, ref, onMounted, watch, onBeforeUnmount } from 'vue'
  import useAiChat from '@/store/aiChat'
  import { storeToRefs } from 'pinia'
  import ChatListItem from './components/chatListItem.vue'
  import ChatHome from './components/chatHome.vue'
  import NewChat from './components/newChat.vue'
  import PromptsList from './components/promptsList.vue'

  const aiChatStore = useAiChat()
  const emit = defineEmits([])
  const { proxy } = getCurrentInstance()
  const props = defineProps({})
  const { newChatFlag, historyChatFlag, allPromptFlag, chatWithRoleFlag, chatRoleInfo, someHistoryInfo } = storeToRefs(aiChatStore)
  const dialogHistoryList = ref([])
  const newChatRef = ref(null)
  /* ========方法========= */
  const getDialogHistoryList = async () => {
    try {
      const res = await proxy.$http.getWithUrl('dialog/query')
      dialogHistoryList.value = res
    } catch (error) {
      console.log(error)
    }
  }
  const jumpToHistoryChat = historyChatInfo => {
    if (!allPromptFlag.value) {
      getDialogHistoryList()
    }
    aiChatStore.jumpToHistoryChat(historyChatInfo)
  }
  const back2Home = () => {
    aiChatStore.initAiChatState()
    getDialogHistoryList()
  }
  const createNewChat = () => {
    aiChatStore.createNewChat()
    newChatRef.value.initChat()
    getDialogHistoryList()
  }
  onMounted(() => {
    getDialogHistoryList()
  })
  onBeforeUnmount(() => {
    aiChatStore.initAiChatState()
  })
</script>

<style scoped lang="scss">
  .ai-chat {
    @include wh100();
    padding: pxTvh(24);
    display: flex;
    justify-content: space-between;
    color: #fff;

    @include computed_font(14);
    & > div {
      box-sizing: border-box;
      height: 100%;
      background-color: #0d172f;
    }
    & > .chat-list-wrapper {
      width: pxTvw(330);
      margin-right: pxTvw(24);
      padding: pxTvh(24) pxTvw(16);
      display: flex;
      flex-direction: column;
      & > .btn-group {
        margin-bottom: pxTvh(32);
      }
      & > .list-title {
        margin-bottom: pxTvh(32);
      }
      & > .chat-list {
        flex: 1;
        overflow-y: scroll;
        padding-right: pxTvw(4);
        & > div:last-child {
          margin-bottom: 0;
        }
      }
    }
    & > .main-chat-wrapper {
      flex: 1;
      overflow: hidden;
    }
  }
</style>
